<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .active {
        color: red;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <h2>todolist</h2>
      <input type="text" v-model="textValue" @keyup.enter="add" />
      <button @click="add">新增</button>

      <ul>
        <li v-for="item in list" :key="item.id" :class="{active: item.checked}">
          <!-- 首先不能绑定全局的变量 -->
          <!-- 可以将所有的复选框作为一组 -->
          <!-- <input type="checkbox" name="ckd" v-model="arr" :value="item.id" /> -->

          <!-- 将要绑定的变量放到数组的每个对象里面 -->
          <input type="checkbox" name="ckd" v-model="item.checked" />
          {{ item.name }} -
          <button @click="remove(item.id)">删除</button>
        </li>
      </ul>

      <!-- <p>{{ arr }}</p> -->
      <button @click="handleRemove">批量删除</button>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const { createApp } = Vue;

      createApp({
        data() {
          return {
            list: [
              {
                id: 1,
                name: "zhangsan",
                checked: false,
              },
              {
                id: 2,
                name: "lisi",
                checked: false,
              },
              {
                id: 3,
                name: "wangwu",
                checked: false,
              },
            ],
            textValue: "",
          };
        },
        methods: {
          add() {
            this.list.push({
              id: new Date().getTime(),
              name: this.textValue,
            });
            this.textValue = "";
          },
          remove(id) {
            this.list = this.list.filter((item) => item.id !== id);
          },
          handleRemove() {
            // console.log(this.list);
            // 将所有checked为true删除
            this.list = this.list.filter((item) => !item.checked);
          },
        },
      }).mount("#app");
    </script>
  </body>
</html>
